import axios from 'axios'

// 配置基础URL
const API_BASE_URL = 'http://localhost:8081'

// 创建axios实例
const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response
  },
  (error) => {
    if (error.response?.status === 401) {
      localStorage.removeItem('token')
      window.location.href = '/login'
    }
    return Promise.reject(error)
  }
)

// 课程管理API
export const courseAPI = {
  // 创建课程
  createCourse: (courseData) => api.post('/api/courses', courseData),
  
  // 获取课程信息
  getCourse: (courseId) => api.get(`/api/courses/${courseId}`),
  
  // 获取所有课程
  getAllCourses: () => api.get('/api/courses'),
  
  // 更新课程信息
  updateCourse: (courseId, courseData) => api.put(`/api/courses/${courseId}`, courseData),
  
  // 删除课程
  deleteCourse: (courseId) => api.delete(`/api/courses/${courseId}`),
  
  // 课程报名
  enrollCourse: (enrollmentData) => api.post('/api/courses/enroll', enrollmentData),
  
  // 获取用户课程
  getUserCourses: (userId, role) => api.get(`/api/courses/users/${userId}/role/${role}`),
  
  // 获取课程章节
  getCourseChapters: (courseId) => api.get(`/api/courses/${courseId}/chapters`),
  
  // 获取课程资料
  getCourseMaterials: (courseId) => api.get(`/api/materials/course/${courseId}`),
  
  // 获取课程教师
  getCourseTeachers: (courseId) => api.get(`/api/courses/${courseId}/teachers`),
  
  // 添加课程教师
  addCourseTeacher: (courseId, teacherId) => api.post(`/api/courses/${courseId}/teachers/${teacherId}`),
  
  // 删除课程教师
  removeCourseTeacher: (courseId, teacherId) => api.delete(`/api/courses/${courseId}/teachers/${teacherId}`),
  
  // 检查课程是否存在
  courseExists: (courseId) => api.get(`/api/courses/${courseId}/exists`)
}

// 章节管理API
export const chapterAPI = {
  // 创建章节
  createChapter: (chapterData) => api.post('/api/chapters', chapterData),
  
  // 获取章节信息
  getChapter: (chapterId) => api.get(`/api/chapters/${chapterId}`),
  
  // 获取课程章节
  getChaptersByCourse: (courseId) => api.get(`/api/chapters/course/${courseId}`),
  
  // 更新章节信息
  updateChapter: (chapterId, chapterData) => api.put(`/api/chapters/${chapterId}`, chapterData),
  
  // 删除章节
  deleteChapter: (chapterId) => api.delete(`/api/chapters/${chapterId}`),
  
  // 检查章节是否存在
  chapterExists: (chapterId) => api.get(`/api/chapters/${chapterId}/exists`)
}

// 资料管理API
export const materialAPI = {
  // 上传资料
  uploadMaterial: (formData) => api.post('/api/materials/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }),
  
  // 获取资料信息
  getMaterial: (materialId) => api.get(`/api/materials/${materialId}`),
  
  // 获取课程资料
  getMaterialsByCourse: (courseId) => api.get(`/api/materials/course/${courseId}`),
  
  // 获取章节资料
  getMaterialsByChapter: (courseId, chapterId) => api.get(`/api/materials/${courseId}/chapter/${chapterId}`),
  
  // 下载资料
  downloadMaterial: (materialId) => api.get(`/api/materials/${materialId}/download`, {
    responseType: 'blob'
  }),
  
  // 删除资料
  deleteMaterial: (materialId) => api.delete(`/api/materials/${materialId}`),
  
  // 更新学生资料状态
  updateStudentMaterialStatus: (data) => api.put('/api/materials/student/status', null, {
    params: data
  }),
  
  // 检查资料是否存在
  materialExists: (materialId) => api.get(`/api/materials/${materialId}/exists`)
}

// 班级管理API
export const classAPI = {
  // 创建班级
  createClass: (classData) => api.post('/api/classes', classData),
  
  // 获取班级信息
  getClass: (classId) => api.get(`/api/classes/${classId}`),
  
  // 获取课程班级
  getClassesByCourse: (courseId) => api.get(`/api/classes/course/${courseId}`),
  
  // 获取教师班级
  getTeacherClasses: (courseId, teacherId) => api.get(`/api/classes/course/${courseId}/teacher/${teacherId}`),
  
  // 获取班级学生
  getClassStudents: (classId) => api.get(`/api/classes/${classId}/students`),
  
  // 获取班级学生ID
  getClassStudentIds: (classId) => api.get(`/api/classes/${classId}/students/ids`),
  
  // 获取班级教师ID
  getClassTeacherIds: (classId) => api.get(`/api/classes/${classId}/teachers/ids`),
  
  // 添加学生到班级
  addStudentToClass: (classId, studentId) => api.post(`/api/classes/${classId}/students/${studentId}`),
  
  // 从班级移除学生
  removeStudentFromClass: (classId, studentId) => api.delete(`/api/classes/${classId}/students/${studentId}`),
  
  // 检查学生是否已加入班级
  checkStudentJoined: (courseId, studentId) => api.get(`/api/classes/course/${courseId}/students/${studentId}/joined`),
  
  // 删除班级
  deleteClass: (classId) => api.delete(`/api/classes/${classId}`),
  
  // 检查班级是否存在
  classExists: (classId) => api.get(`/api/classes/${classId}/exists`)
}

export default api
